package com.scalar.chiptrack.operations.finishedGoods.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.finishedGoods.actionCommands.FinishedGoodsActionCommandFactory;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.Logger;

/**
 * User: Murali Krishna Yengala
 * Date: May 27, 2005
 * Time: 10:54:48 AM
 */
public class FinishedGoodsEntryAction extends ChipTrackExtendedAction{

    private static final String ACTION_NAME = "/fgSearch.do";
    public ActionForward executeRequest( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );

        ActionForward actionForward = mapping.findForward("fgSearch");
        String action = request.getParameter(REQUEST_ACTION);
        action = (action == null ? "fgSearch" : action.trim());
        //System.out.println("action : " + action);
        HttpSession session = request.getSession();

        User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
        if ( user != null && user.isActionAllowed ( Functionality.FINISHEDGOODS_SETUP, User.USER_ACTION_MODIFY ) )
        {
            request.setAttribute ( "showMessage", "FALSE" );
        }
        else
        {
            request.setAttribute ( "showMessage", "TRUE" );
        }
        try
        {
            ChipTrackExtendedAction actionCommand = FinishedGoodsActionCommandFactory.getActionCommand( action );
            if ( actionCommand != null)
            {
                actionForward = actionCommand.executeRequest( mapping, form, request, response );
            }
            else
            {
                Logger.error( "" );
                errors.put( ERROR_INTERNAL_SERVER_ERROR, "Couldn't create Finished Goods Action instance" );
                request.setAttribute( ERROR_MESSAGES, errors );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        return actionForward;
    }
}
